﻿using System.CodeDom;
using System.Collections;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using EntityFramework6.Services.Data;

namespace EntityFramework6.Services.Services
{
    public class BookService : IBookService
    {
        private readonly BookContext _Context;

        public BookService(BookContext context)
        {
            _Context = context;
        }

        public int Save(Book book)
        {
            _Context.Books.Add(book);
            _Context.SaveChanges();
            return book.ID;
        }

        public void Delete(int id)
        {
            var bookToDelete = _Context.Books.FirstOrDefault(b => b.ID.Equals(id));
            _Context.Entry(bookToDelete).State = EntityState.Deleted;
            _Context.SaveChanges();
        }

        public Book Read(int id)
        {
            return _Context.Books.FirstOrDefault(b => b.ID.Equals(id));
        }

        public IList<Book> List()
        {
            var books = _Context.Books.ToList();
            return books;
        }
    }

    public interface IBookService
    {
        int Save(Book book);
        void Delete(int id);
        Book Read(int id);
        IList<Book> List();
    }
}
